package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @PostMapping
    public R save (@RequestBody SetmealDto setmealDto){
         setmealService.saveSetmealAndSetmealDish(setmealDto);
         return R.success(null);
    }

    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name){
        Page<SetmealDto> setmealDtoPage =setmealService.pageAll(page,pageSize,name);
        return R.success(setmealDtoPage);
    }
    @DeleteMapping
    public R remove(@RequestParam List<Long> ids){
        setmealService.deleteSetmealAndSetmealDish(ids);
        return R.success(null);
    }

    /**
     * 根据条件查询套餐数据:categoryid,status
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);
        List<Setmeal> list = setmealService.list(setmealLambdaQueryWrapper);
        return R.success(list);
    }
    @GetMapping("/dish/{id}")
    public R<List<DishDto>> dishInfo(@PathVariable Long id){
        List<DishDto> list =setmealService.listDishDto(id);
        return R.success(list);
    }
}
